package byj.com.meishi.utils;

import android.content.Context;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2016/9/19 0019.
 */
public class OkHttpClientUtils {
    private static OkHttpClient okHttpClient;
    private static OkHttpClientUtils okHttpClientUtils;

    //对OkHttpClient进行初始化，调用getOkHttpClient()得到okHttpClient对象
    private OkHttpClientUtils(Context context){
        okHttpClient=getOkHttpClient();
        //设置
        okHttpClient.newBuilder().connectTimeout(20, java.util.concurrent.TimeUnit.SECONDS);
        okHttpClient.newBuilder().writeTimeout(20, TimeUnit.SECONDS);
        okHttpClient.newBuilder().readTimeout(15,TimeUnit.SECONDS);
        //设置缓存大小
        int size=1024 * 1024 * 10;//10M
        //根据上下文得到缓存对象，并对其大小进行设置
        Cache cache=new Cache(context.getCacheDir(),size);
        //设置缓存
        okHttpClient.newBuilder().cache(cache);
    }
    //2.创建OkHttpClient对象
    public static OkHttpClient getOkHttpClient(){
        if(okHttpClient == null){
            synchronized (OkHttpClient.class){
                okHttpClient=new OkHttpClient();
            }
        }
        return okHttpClient;
    }
    //1.单例模式得到OkHttpClientUtils对象，并在其中对OkHttpClient进行初始化，

    public static OkHttpClientUtils getOkHttpClientUtils(Context context){
        if(okHttpClientUtils == null){
            synchronized (OkHttpClientUtils.class){
                okHttpClientUtils=new OkHttpClientUtils(context);
            }
        }
        return  okHttpClientUtils;
    }
    ///////////////////////////////////////////////////////////////////////////
    // GET同步网络请求
    ///////////////////////////////////////////////////////////////////////////
    private Request getRequest(String url){
        //根据url得到一个Request对象
        Request request=new Request.Builder().url(url).build();
        return  request;
    }
    private Response getResponse(String url) throws IOException {
        //调用getRequest(url)方法得到reque对象
        Request request = getRequest(url);
        //根据reque对象，请求数据，并返回响应码
        Response response = okHttpClient.newCall(request).execute();
        return response;
    }

    private ResponseBody getResonseBody(String url) throws IOException {
        //得到返回响应码
        Response response = getResponse(url);
        // 根据响应码，来判断是否得到数据
        if(response.isSuccessful()){
            ResponseBody body = response.body();
            return body;
        }
        return  null;
    }

    //返回值为String类型
    public static String getStringFromUrl(String url) throws IOException {
        ResponseBody body = okHttpClientUtils.getResonseBody(url);
        if(body!=null){
            String result = body.string();
            return  result;
        }
        return  null;
    }
    //返回值为byte[]类型
    public static byte[] getBytesFromUrl(String url) throws IOException {
        ResponseBody body = okHttpClientUtils.getResonseBody(url);
        if(body!=null){
            byte[] bytes = body.bytes();
            return bytes;
        }
        return null;
    }
    //返回值为字节流类型
    public static InputStream getStreamFromUrl(String url) throws IOException {
        ResponseBody body = okHttpClientUtils.getResonseBody(url);
        if(body!=null){
            InputStream stream = body.byteStream();
            return stream;
        }
        return  null;
    }
    ///////////////////////////////////////////////////////////////////////////
    // GET异步网络请求
    ///////////////////////////////////////////////////////////////////////////
    public static void getDataAsync(String url, Context context, Callback callback){
        Request request = getOkHttpClientUtils(context).getRequest(url);
        okHttpClient.newCall(request).enqueue(callback);

    }

}
